Animated Portfolio Gallery with jQuery
Tutorials November 14, 2010 by Mary Lou
79 
View demoDownload source
Today
we will create an animated portfolio gallery with jQuery. The gallery
will contain a scroller for thumbnails and a content area where we will
display details about the portfolio item. The image can be enlarged by
clicking on it, making it appear as an overlay.
The idea is to
animate the content elements whenever a thumbnail is clicked. We will
animate the heading from the top, fade out the previous image and slide
the descriptions from the sides.
The great template collection for the demo can be found on DzineBlog.
Let’s start with the HTML structure.
The Markup
First
we will create the structure for the content area. For that we will
create a main div element with the class “pg_content”. Inside we will
place the main heading with the class and id “pg_title”, the preview
container with the id “pg_preview” and the description elements with the
class “pg_description”. The description divs will also have an
individual class, i.e. “pg_desc1″ and “pg_desc2″:
01 | <div class="pg_content"> |
02 | <div id="pg_title" class="pg_title"> |
03 | <h1 style="display:block;top:25px;"> |
04 | Shape Company Website Design |
06 | <h1>Summer of Love</h1> |
10 | <img class="pg_thumb" style="display:block;z-index:9999;" src="images/medium/1.jpg" alt="images/large/1.jpg"/> |
11 | <img class="pg_thumb" src="images/medium/2.jpg" alt="images/large/2.jpg"/> |
14 | <div id="pg_desc1" class="pg_description"> |
15 | <div style="display:block;left:250px;"> |
16 | <h2>Project Description</h2> |
17 | <p>A description comes here</p> |
20 | <h2>Project Description</h2> |
21 | <p>A description comes here</p> |
25 | <div id="pg_desc2" class="pg_description"> |
26 | <div style="display:block;left:250px;"> |
27 | <h2>Technologies Used</h2> |
28 | <p>A text comes here</p> |
31 | <h2>Technologies Used</h2> |
32 | <p>A text comes here</p> |
All
the first elements inside of those containers will have a specific
inline style, making them visible when we load the page. The rest of the
elements will have their style defined in the CSS, i.e. their position
when they are hidden.
The thumbnails scroller will have the following structure (based on Manos Malihu’s thumbnail scroller):
01 | <div id="thumbContainter"> |
02 | <div id="thumbScroller"> |
03 | <div class="container"> |
07 | <img src="images/thumbs/1.jpg" alt="" class="thumb" /> |
And finally, we will add a div for the semi-transparent overlay:
1 | <div id="overlay"></div> |
Let’s take a look at the style.
The CSS
We start by resetting the margins and paddings, and by the general body style:
06 | background: #564c4c url(../webtreats2.jpg) repeat top left; |
07 | font-family:"Trebuchet MS","Myriad Pro", Helvetica, sans-serif; |
This and more beautiful background images can be found on http://webtreats.mysitemyway.com.
The overlay is going to have the following style:
If you want this to work in IE, please add the filter opacity property.
The style for the thumbnail slider will be the following:
09 | background:transparent url(../bg.png) repeat top left; |
10 | border-right:1px solid #f0f0f0; |
11 | -moz-box-shadow:-2px 0px 10px #000 inset; |
12 | -webkit-box-shadow:-2px 0px 10px #000 inset; |
13 | box-shadow:-2px 0px 10px #000 inset; |
21 | #thumbScroller .container{ |
26 | #thumbScroller .content{ |
30 | #thumbScroller .content div{ |
35 | #thumbScroller .content a{ |
39 | border:5px solid #000; |
40 | -moz-box-shadow:0px 0px 2px #000; |
41 | -webkit-box-shadow:0px 0px 2px #000; |
42 | box-shadow:0px 0px 2px #000; |
All the elements in the content area will be placed absolutely:
4 | .pg_content .pg_description div |
The description divs will be not be displayed initially:
1 | .pg_content .pg_description div{ |
The
main heading will have a black patterned background image, just like
the main thumbnail container. The position will be set to the values
where the element will not be visible, hence we will have a top of
-50px:
5 | background:transparent url(../bg.png) repeat top left; |
We will then animate the top to 25px to make the heading visible.
The thumb needs to be hidden, too:
The
large preview of the image will be created when we click on a portfolio
item in the content area. It will have the highest z-index:
The style of the thumbnail in the content area and the style of the large preview will be as follows:
6 | background:transparent url(../bg.png) repeat top left; |
The descriptions and their headlines are going to have the following style:
05 | background:transparent url(../bg2.png) repeat top left; |
13 | text-shadow:0px 0px 1px #fff; |
14 | background:transparent url(../bg.png) repeat top left; |
and finally we need to define the positions of the description divs:
And that was the style. Let’s check out the JavaScript next.
The JavaScript
The
main idea is to create the scrollable thumbnails container, and to
provide the transitions of the content elements. Also, we need to define
what happens when we click on a medium image in the content area to
view the large image preview.
First, let’s define some variables:
06 | var easeType = 'easeOutCirc'; |
08 | var $thumbScroller = $('#thumbScroller'); |
09 | var $scrollerContainer = $thumbScroller.find('.container'); |
10 | var $scrollerContent = $thumbScroller.find('.content'); |
11 | var $pg_title = $('#pg_title'); |
12 | var $pg_preview = $('#pg_preview'); |
13 | var $pg_desc1 = $('#pg_desc1'); |
14 | var $pg_desc2 = $('#pg_desc2'); |
15 | var $overlay = $('#overlay'); |
17 | var scrollerContentCnt = $scrollerContent.length; |
18 | var sliderHeight = $(window).height(); |
Then, we want to create the scrollable thumbnails container, after all its thumbnail images are loaded:
02 | $thumbScroller.find('img').each(function(){ |
04 | $('<img alt="">').load(function(){ |
06 | if(cnt == scrollerContentCnt){ |
08 | itemHeight = $thumbScroller.find('.content:first').height(); |
09 | buildScrollableItems(); |
11 | $thumbScroller.stop().animate({ |
15 | }).attr('src',$img.attr('src')); |
When
we click on a thumbnail in the scrollable container, we want to display
the according content elements. We will use the index of the thumbnail
in order to know which title, image and description to show:
01 | $scrollerContent.bind('click',function(e){ |
04 | var idx = $this.index(); |
06 | if(current==idx) return; |
11 | var $pg_large = $('#pg_large'); |
12 | if($pg_large.length > 0){ |
16 | },animSpeed,function(){ |
22 | var $currentTitle = $pg_title.find('h1:nth-child('+(current+1)+')'); |
23 | var $nextTitle = $pg_title.find('h1:nth-child('+(idx+1)+')'); |
24 | var $currentThumb = $pg_preview.find('img.pg_thumb:eq('+current+')'); |
25 | var $nextThumb = $pg_preview.find('img.pg_thumb:eq('+idx+')'); |
26 | var $currentDesc1 = $pg_desc1.find('div:nth-child('+(current+1)+')'); |
27 | var $nextDesc1 = $pg_desc1.find('div:nth-child('+(idx+1)+')'); |
28 | var $currentDesc2 = $pg_desc2.find('div:nth-child('+(current+1)+')'); |
29 | var $nextDesc2 = $pg_desc2.find('div:nth-child('+(idx+1)+')'); |
36 | $currentTitle.stop().animate({ |
38 | },animSpeed,function(){ |
40 | $nextTitle.show().stop().animate({ |
49 | $currentThumb.stop().animate({ |
52 | },animSpeed,function(){ |
65 | $currentDesc1.stop().animate({ |
68 | },animSpeed,function(){ |
70 | $nextDesc1.show().stop().animate({ |
75 | $currentDesc2.stop().animate({ |
78 | },animSpeed,function(){ |
80 | $nextDesc2.show().stop().animate({ |
When
we click on a thumbnail in the content area (the medium picture), it
will animate to the size of the large image. Then we will load the large
image and insert it after the thumbnail. We just need to hide the
thumbnail then, in order to display the large image:
01 | $pg_preview.find('.pg_thumb').bind('click',showLargeImage); |
04 | function showLargeImage(){ |
06 | $('#pg_large').remove(); |
08 | $thumb.unbind('click'); |
09 | var large_src = $thumb.attr('alt'); |
13 | $thumb.stop().animate({ |
17 | $('<img id="pg_large">').load(function(){ |
18 | var $largeImg = $(this); |
19 | $largeImg.insertAfter($thumb).show(); |
29 | $largeImg.bind('click',function(){ |
31 | $overlay.fadeOut(200); |
32 | $(this).stop().animate({ |
37 | $thumb.css({'z-index' : 9999}); |
39 | $thumb.bind('click',showLargeImage); |
43 | }).attr('src',large_src); |
When we resize the window, the scroller’s height needs to be adapted to the new window height:
1 | $(window).resize(function() { |
2 | var w_h = $(window).height(); |
3 | $thumbScroller.css('height',w_h); |
And finally, the function for creating the scroller container (adapted from Manos’ script, check it out here: Manos Malihu’s thumbnail scroller)
01 | function buildScrollableItems(){ |
02 | totalContent = (scrollerContentCnt-1)*itemHeight; |
03 | $thumbScroller.css('height',sliderHeight) |
04 | .mousemove(function(e){ |
05 | if($scrollerContainer.height()>sliderHeight){ |
06 | var mouseCoords = (e.pageY - this.offsetTop); |
07 | var mousePercentY = mouseCoords/sliderHeight; |
08 | var destY
=
-(((totalContent-(sliderHeight-itemHeight))-sliderHeight)*(mousePercentY)); |
09 | var thePosA = mouseCoords-destY; |
10 | var thePosB = destY-mouseCoords; |
11 | if(mouseCoords==destY) |
12 | $scrollerContainer.stop(); |
13 | else if(mouseCoords>destY) |
14 | $scrollerContainer.stop() |
And
that’s all! If you want to add some spice by cufonizing the headlines,
you can add the following lines to the head of your HTML:
1 | <script src="js/cufon-yui.js" type="text/javascript"></script> |
2 | <script src="js/Quicksand_Book_400.font.js" type="text/javascript"></script> |
3 | <script type="text/javascript"> |
4 | Cufon.replace('h1,h2'); |
You can find the beautiful Quicksand and other awesome fonts on www.fontsquirrel.com.
We hope you enjoyed the tutorial and find it useful!
View demoDownload source
Tagged with: animated, gallery, images, jQuery, portfolio
Awesome, very clean javascript code. Love it, great work!
Amazing work, as usual
Keep up the great work
I have downloaded your script but in it Left thumbnail panel not displaying can you check it and give me proper download zip then we appreciate it , bye the way gr8 work thanks for sharing..
Woowww….
Realy nice work, thnx 4 ur effort
The download package isn’t working.
Great as always…
Clean Jquery code
Nice Html code
Thanks Mary Lou
I am using Chrome and i see basiaclly the same bug i wrote about in one of the other tutorials, full page image gallery tutorial . . .
I can’t believe this is too gud. I like the work may b i’d like to use it on my website.
@Meera @Catalin I have updated the ZIP file and now it is working! Sorry for that! Thank you all for your great feedback! Cheers, ML
a little difficult in the right clicking on the scroll boss?
if we click a picture, how do I return again?
awesome job…ty \m/
WOW!
This is so beautifiul!
Another outstanding example of jQuery effect shared with us. Thanks so much!
very nice
Looks great in Chrome and Safari, but Firefox 4.0b7 for mac is looking completely glitched for me.
Anybody else getting this?
Great tutorial though, as usual!
This is so beautifiul!
Awesome…as ever….
Great !
Very good thing…
great as always…..
I noticed that you didn’t have a license anywhere on your code. I’m assuming this is free to use?? (Weird question, I know but I need to ask for work. :-) )
Great work by the way.
Thank you all for your great feedback!
@Mike, yes it is free for personal and commercial use, thanks for asking!
This is beautiful
Great Job
Nice, thanks !
Awesome, Awesome, Awesome, Awesome. and Awesome (again :).
I really liked your work.
hi just want to ask if i want to add some more thumails and pages wht i hav to do. Plz if u can let me soon it will be great.
Hey,
Can I just directly use this as an internal static page in my wordpress based website?! A total cipher in web programming…
very very good
tanks
who needs varsity…? ..i got it all here
Really great script! I want to add previous en next buttons can someone help me?
Converted Portfolio in Joomla Version check it http://bit.ly/i0S6kM
Hey Mary Lou,
Your Blog is My Design Inspiration ^_^
Like This ^_^
Converted Animated Portfolio Gallery with jQuery to Blogger »»» http://assadesign.blogspot.com/
thanks for sharing this nice tutorial it is more helpful to all keep sharing
i have started to do this portfolio.. so many doubt